<?php
/***
 * This is the user login script.  It will take any new user to your auth server
 * and create an account for them after cheching with the Minecraft.net servers
 * to verify that the user has an account with a corect password.  Any returning
 * user will just be logged in normally.
 */

require_once "scripts/config.php";
require_once "scripts/functions.php";

/**
 * ATTENTION THERE IS SOMETHING TO MODIFY HERE
 * If you want to redirect users to specific pages after authentication then set
 * the url's to thoes pages below.  Otherwise just a generic page with "Success or Failed"
 * will show up
 */

$userAuthenticationSuccessPage = "loginsuccess.html";
$userAuthenticationFailurePage = "loginfail.html";

/* Check for user in database */
if(userExists($_POST['username'])){
    if(authenticateUser($_POST['username'], $_POST['password'])){
        /* User is logged in */
        header("Location:".$userAuthenticationSuccessPage);
    } else {
        /* Bad password */
        header("Location:".$userAuthenticationFailurePage);
    }
} else {
    /* User does not exsist check Minecraft.net */
    if(checkMinecraftForUser($_POST['username'], $_POST['password'])){
        if(createNewUser($_POST['username'], $_POST['password'])){
            header("Location:".$userAuthenticationSuccessPage);
        } else {
            header("Location:".$userAuthenticationFailurePage);
        }
    } else {
        header("Location:".$userAuthenticationFailurePage);
    }
}


?>